﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using YSC_App_WebApi.RestClient;
using YSC_Core.BLL.Domain.CommonModels;
using YSC_Core.BLL.Domain.Utils;
using YSC_Core.BLL.Domain.ViewModels;
using YSC_Services.BLL.Services.Interfaces;



namespace YSC_App_WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private IUserAppServices _rep;
        public UserController(IUserAppServices rep)
        {
            this._rep = rep;
        }


        [HttpGet]
        [Route("GetTest")]
        public ActionResult GetTest()
        {
            Dictionary<string, string> postData = new Dictionary<string, string>();

            postData.Add("HCPId", "0019000000sKZyuAAG");
            postData.Add("UnionId", "o-trx1PQiJNbZ8O4HTx-98KoA08I");
            postData.Add("CountryCode", "CN");

            // 自己实现的http请求
            string postStr = HttpUtils.DictoryToGetParam(postData);

            var teststr = new
            {
                HCPId = "0019000000sKZyuAAG",
                UnionId = "o-trx1PQiJNbZ8O4HTx-98KoA08I",
                CountryCode = "CN"
            };

            string urls = @"https://bisbx-medcomms.veevavault.com/api/v15.0/objects/documents";

            var url = "https://api-dev.boehringer-ingelheim.com:8443/veevawechat/api/HCPSocialMediaUpdate";
            try
            {
                var repose = HttpUtils.HttpGet(urls);
                RootModel tokenModel = JsonConvert.DeserializeObject<RootModel>(repose);
                List<Document> list = new List<Document>();
                //for (int i = 0; i < tokenModel.documents.Count; i++)
                //{
                //    Document document = tokenModel.documents[i].document;
                //    list.Add(document);

                //}
                tokenModel.documents.ForEach(p => list.Add(p.document));

              //  var a = list.Where(r=>r.name__v.Contains("china")|| r.name__v.Contains("China")).ToList();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(ex.ToString());
            }


        }



        /// <summary>
        /// Ids用户通信
        /// </summary>
        /// <param name="userName">账户</param>
        /// <param name="passWord">密码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetOAuthUser")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public ActionResult GetOAuthUser(string userName, string passWord)
        {
            var ret = _rep.OAuthUser(userName, passWord);
            return Ok(ret);
        }

        /// <summary>
        /// Ids用户通信
        /// </summary>
        /// <param name="userid">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("OAuthUserId")]
        [Authorize]
        [ApiExplorerSettings(IgnoreApi = true)]
        public ActionResult OAuthUserId(string userid)
        {
            var ret = _rep.OAuthUserId(userid);
            return Ok(ret);
        }

        /// <summary>
        /// 账户密码登陆
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Login")]
        //  [TestAuthorize("User_Edit")]
        public ActionResult Login(LoginModel login)
        {
            //  var items = (HttpContext.User.Identity as ClaimsIdentity).Claims;
            var ret = _rep.Login(login.UserName, login.PassWord);
            return Ok(ret);
            //   if ((HttpContext.User.Identity as ClaimsIdentity).Claims.Count() > 0)
            // return new JsonResult(from c in HttpContext.User.Claims select new { c.Type, c.Value });
            // var item = (HttpContext.User.Identity as ClaimsIdentity);

            //var items = AuthorizationFilterContext.HttpContext.User.Claims;
            //      var userId = (HttpContext.User.Identity as ClaimsIdentity).Claims.FirstOrDefault(p => p.Type == "Id").Value;

            //  var ret = _rep.Login(userName, passWord);
            //   return Ok(ret);
        }

        /// <summary>
        /// 验证码发送
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("SMSCode")]
        public ActionResult SMSCode(string phone)
        {
            var ret = _rep.SMSCode(phone);
            return Ok(ret);
        }

        /// <summary>
        /// 短信登陆
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SMSLogin")]
        public ActionResult SMSLogin(SMSLoginModel login)
        {
            var ret = _rep.SMSLogin(login.Phone, login.SMSCode);
            return Ok(ret);
        }

        /// <summary>
        /// 微信登陆
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("WXLogin")]
        public ActionResult WXLogin(WXModel model)
        {
            var ret = _rep.WXLogin(model.encryptedDataStr, model.key, model.iv);
            return Ok(ret);
        }

        /// <summary>
        /// 微信注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("WXRegistered")]
        public ActionResult WXRegistered(WXRegisteredModel model)
        {
            var ret = _rep.WXRegistered(model);
            return Ok(ret);
        }

        /// <summary>
        /// 个人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUser")]
        [TestAuthorize("User_Edit")]
        public ActionResult GetUser()
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                userid = items.Value;
            var ret = _rep.GetUser(userid);
            return Ok(ret);
        }


        /// <summary>
        /// 个人信息修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns>年龄/性别 必填</returns>
        [HttpPost]
        [Route("UpdateUser")]
        [TestAuthorize("User_Edit")]
        public ActionResult UpdateUser(YscUserModel model)
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                model.Id = items.Value;

            var ret = _rep.UpdateUser(model);
            return Ok(ret);
        }

        /// <summary>
        /// 个人媒体信息修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateUserMedia")]
        [TestAuthorize("User_Edit")]
        public ActionResult UpdateUserMedia(YscUserMediaModel model)
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                model.Id = items.Value;

            var ret = _rep.UpdateUserMedia(model);
            return Ok(ret);
        }

        /// <summary>
        /// 验证码验证
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SMSVerification")]
        public ActionResult SMSVerification(YscUserMSM user)
        {
            var ret = _rep.SMSVerification(user.smsCode, user.Phone);
            return Ok(ret);
        }

        /// <summary>
        /// 用户密码修改(找回)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateUserPassword")]
        public ActionResult UpdateUserPassword(YscUserPasswordModel user)
        {
            var ret = _rep.UpdateUserPassword(user.Phone, user.Password);
            return Ok(ret);
        }

        /// <summary>
        ///  用户认证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddUserAttest")]
        [TestAuthorize("User_Edit")]
        public ActionResult AddUserAttest(UserAttestModel model)
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                model.UserId = items.Value;
            // model.UserId = "3766b4d6-dc87-4636-9454-781199b199f8";
            var ret = _rep.AddUserAttest(model);
            return Ok(ret);
        }

        /// <summary>
        /// 个人历史报价查询
        /// </summary>
        /// <param name="input"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetUserHistoryPriceAll")]
        [TestAuthorize("User_Edit")]
        public ActionResult GetUserHistoryPriceAll(PaginationRequest input, string dateTime)
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                userid = items.Value;

            var ret = _rep.GetUserHistoryPriceAll(input.Page, input.PageSize, userid, dateTime);
            return Ok(ret);
        }


        /// <summary>
        /// 非认证用户历史报价查询
        /// </summary>
        /// <param name="input"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetUserHistoryPriceYKAll")]
        [TestAuthorize("User_Edit")]
        public ActionResult GetUserHistoryPriceYKAll(PaginationRequest input, string dateTime)
        {
            var claimsAll = HttpContext.User.Claims.Select(p => new { p.Type, p.Value });
            var items = claimsAll.FirstOrDefault(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

            var userid = string.Empty;

            if (items != null)
                userid = items.Value;

            var ret = _rep.GetUserHistoryPriceYKAll(input.Page, input.PageSize, userid, dateTime);
            return Ok(ret);
        }

        [HttpGet]
        [Route("GetText")]
        public ActionResult GetText()
        {
            // if ((HttpContext.User.Identity as ClaimsIdentity).Claims.Count() > 0)
            return Ok(new JsonResult(from c in HttpContext.User.Claims select new { c.Type, c.Value }));
        }

    }
}